package com.carrent.demo.Controller;

import com.carrent.demo.Domain.Cars;
import com.carrent.demo.Domain.ContractDto;
import com.carrent.demo.Domain.EmployeeInformation;
import com.carrent.demo.Domain.Users;
import com.carrent.demo.Service.CarsService;
import com.carrent.demo.Service.UsersService;
import com.carrent.demo.Utils.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarsController {
    @Autowired
    CarsService carsService;
    //页面查询汽车信息
    @GetMapping("/page")
    public R getPage(Integer page,Integer pageSize,String name)
    {
        final List<Cars> page1 = carsService.getPage((page - 1) * pageSize, pageSize, name);
        final Integer count = carsService.getCount(name);
        return  R.success(page1,count);
    }
    //页面获取汽车信息
    @GetMapping("/page2")
    public R getPage2(Integer page,Integer pageSize)
    {
        final List<Cars> page2 = carsService.getPage2((page - 1) * pageSize, pageSize);
        final Integer count2 = carsService.getCount2();
        return  R.success(page2,count2);
    }
    //页面删除车辆信息
    @DeleteMapping
    public  R deleteCars(@RequestParam(value = "ids") List<String> ids)
    {
        for (String id : ids)
        {
            carsService.deleteCar(id);
        }
        return  R.success("");
    }
    //修改汽车租赁状态
    @PostMapping("/status/{status}")
    public  R editStutus(@PathVariable Integer status,@RequestParam(value = "ids") List<Integer> ids)
    {
        for (Integer i : ids)
        {
            carsService.editStatus(i,status);
        }
        return  R.success("");
    }
    //更新汽车信息
    @PutMapping
    public  R editCar(@RequestBody Cars cars)
    {
        carsService.editCar(cars);
        return  R.success(1);
    }
    //新增汽车信息
    @PostMapping
    public  R addCar(@RequestBody Cars cars)
    {
        final Boolean aBoolean = carsService.addCar(cars);
        return  R.success(aBoolean);
    }

    @GetMapping("/getEmployee")
    public  R selectEmployee()
    {
        final List<EmployeeInformation> list = carsService.selectEmployee();
        return  R.success(list);
    }
    @PostMapping("/contract")
    public R addContract(@RequestBody ContractDto contractDto, HttpServletRequest htr) throws ParseException {
        final String usersId =htr.getSession().getAttribute("users").toString();

       //  System.out.println(contractDto.getStartDate());
          contractDto.setEndDate( contractDto.getEndDate().substring(0, 10));
          contractDto.setStartDate(contractDto.getStartDate().substring(0,10));
          carsService.addContract(contractDto,usersId);
        return R.success("");
    }

}